package com.baizhi.action.front;

import com.baizhi.entity.Book;
import com.baizhi.entity.Cart;
import com.baizhi.entity.CartItem;
import com.baizhi.service.BookService;
import com.baizhi.service.impl.BookServiceImpl;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class CartAction {

    private String bookId;
    private Integer count;
    private Integer itemCount;

    public String getBookId() {
        return bookId;
    }

    public void setBookId(String bookId) {
        this.bookId = bookId;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Integer getItemCount() {
        return itemCount;
    }

    public void setItemCount(Integer itemCount) {
        this.itemCount = itemCount;
    }

    public String addBookToCart(){
        BookService bookService=new BookServiceImpl();
        HttpServletRequest req = ServletActionContext.getRequest();
        HttpSession session = req.getSession();
        Cart cart = (Cart) session.getAttribute("cart");

        //第一次使用购物车
        if (cart==null){
            cart=new Cart();
            session.setAttribute("cart",cart);
        }

        Map<String,CartItem> items=cart.getItems();
        if(items==null){
            items=new HashMap<String,CartItem>();
            cart.setItems(items);
        }
        if (items.containsKey(bookId)){
            //购物车中有该条目更改数据count+1
            CartItem cartItem = items.get(bookId);
            cartItem.setCount(cartItem.getCount()+1);
        }else {
            CartItem cartItem=new CartItem();
            Book book = bookService.selectBookById(bookId);
            cartItem.setCount(1);
            cartItem.setBook(book);
            items.put(bookId,cartItem);
        }

        double totalPrice=0.0;
        double totalSavePrice=0.0;
        Collection<CartItem> values = items.values();
        for (CartItem cartItem : values) {
            double price=cartItem.getBook().getPrice();//原价
            double dprice=cartItem.getBook().getDprice();//当当价

            totalPrice+=dprice*cartItem.getCount();
            totalSavePrice+=(price-dprice)*cartItem.getCount();
        }
        cart.setTotalPrice(totalPrice);
        cart.setTotalSavePrice(totalSavePrice);

        return "showCart";
    }

    public String deleteCartItem(){
        HttpServletRequest req = ServletActionContext.getRequest();
        HttpSession session = req.getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        if (cart==null){
            return "showCart";
        }
        Map<String, CartItem> items = cart.getItems();
        items.remove(bookId);

        double totalPrice=0.0;
        double totalSavePrice=0.0;
        Collection<CartItem> values = items.values();
        for (CartItem cartItem : values) {
            double price=cartItem.getBook().getPrice();//原价
            double dprice=cartItem.getBook().getDprice();//当当价

            totalPrice+=dprice*cartItem.getCount();
            totalSavePrice+=(price-dprice)*cartItem.getCount();
        }
        cart.setTotalPrice(totalPrice);
        cart.setTotalSavePrice(totalSavePrice);

        return "showCart";
    }

    public String updateCartItemCount(){
        HttpServletRequest req = ServletActionContext.getRequest();
        HttpSession session = req.getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        if (cart==null){
            return "showCart";
        }
        Map<String, CartItem> items = cart.getItems();

        if (count<=0){
            items.remove(bookId);
        }else {
            CartItem cartItem = items.get(bookId);
            cartItem.setCount(count);
        }
        double totalPrice=0.0;
        double totalSavePrice=0.0;
        Collection<CartItem> values = items.values();
        for (CartItem cartItem : values) {
            double price=cartItem.getBook().getPrice();//原价
            double dprice=cartItem.getBook().getDprice();//当当价

            totalPrice+=dprice*cartItem.getCount();
            totalSavePrice+=(price-dprice)*cartItem.getCount();
        }
        cart.setTotalPrice(totalPrice);
        cart.setTotalSavePrice(totalSavePrice);
        return "showCart";
    }
}
